﻿using lwzlz.Application.System;
using lwzlz.Core;
using lwzlz.Web.Areas.SysAdmin.Models;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.SysAdmin.Controllers
{
    public class PlaceController : AdminController
    {
        #region 首页
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region 获取首页列表数据
        [HttpPost]
        public JsonResult GetList()
        {
            PlaceService service = new PlaceService();
            LayTableModel result = new LayTableModel();//layui表格返回数据格式
            try
            {
                int page = int.Parse(Request.Params["page"]);//当前页码
                int limit = int.Parse(Request.Params["limit"]);//每页数量

                #region 参数

                //参数
                string Name = Request.Params["Name"];//当前菜单id

                string sql = @"select * from Sys_Place ";
                string where = "";

                //数据库参数
                Dictionary<string, object> param = new Dictionary<string, object>();

                //状态
                if (!string.IsNullOrWhiteSpace(Name))
                {
                    param.Add("@Name", Name);
                    where += "WHERE Name like '%'+@Name+'%' ";
                }
                #endregion

                //排序sql语句拼接
                string orderby = " ID DESC ";
                sql = sql + where;

                var list = service.GetDynamicPaged(page, limit, orderby, sql, param);
                var count = service.RecordQueryCount(sql, param);


                result.data = list;
                result.count = count;
            }
            catch (Exception ex)
            {
                result.code = -1;
                result.msg = ex.Message;
            }
            return Json(result, JsonRequestBehavior.DenyGet);
        }
        #endregion

        #region 添加和修改页面
        public ActionResult AddOrEditView(int ID = 0)
        {
            Place model = null;
            PlaceService service = new PlaceService();
            if (ID > 0)
            {
                model = service.Get(ID);
            }
            else
            {
                model = new Place();
                model.ID = 0;
            }
            return View(model);
        }
        #endregion

        #region 添加和修改行为
        [HttpPost]
        public JsonResult AddOrEdit(Place Model)
        {
            ResponseModel response = new ResponseModel();
            PlaceService service = new PlaceService();
            response.Success = false;
            int? count = 0;
            try
            {
                if (Model.ID > 0)
                {
                    Place model = service.Get(Model.ID);
                    model.Code = Model.Code;
                    model.ParentID = Model.ParentID;
                    model.Deep = Model.Deep;
                    model.Initials = Model.Initials;
                    model.Hot = Model.Hot;
                    count = service.UpDataSave(model);
                }
                else
                {
                    count = service.Add(Model);
                }
                if (count > 0)
                {
                    response.Success = true;
                    response.Message = "操作成功";
                }
                else
                {
                    response.Success = false;
                    response.Message = "操作失败";
                }
            }
            catch (Exception ee)
            {
                response.Success = false;
                response.Message = "失败：" + ee.Message.ToString();
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }
        #endregion

        #region 删除
        [HttpPost]
        public JsonResult DeleteList(string IdList = "")
        {
            ResponseModel response = new ResponseModel();
            PlaceService serviece = new PlaceService();

            try
            {
                serviece.DeleteList(IdList);
                response.Success = true;
                response.Message = "删除成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response, JsonRequestBehavior.DenyGet);
        }
        #endregion

        #region 获取相应的地区数据
        public JsonResult GetPlace(string Pid = "0")
        {
            var list = new PlaceService().GetSelectList(Pid);
            return Json(list);
        }
        #endregion

    }
}